package com.linkage.dbunit;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.linkage.user.pojo.User;

/**
 * <文件描述>.
 * <p/>
 * Author:zhaozw Date:2015/1/12,15:16 Version:v1.0
 */
public class DbHelper {

    private static final String DB_URL = "jdbc:mysql://localhost:3306/demo_user";
    private static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
    private static final String USER_NAME = "root";
    private static final String PASSWORD = "password";

    private static Connection conn;
    private static ResultSet rs;
    private static PreparedStatement pstmt;

    public static Connection getConnection() {
        try {
            Class.forName(DRIVER_NAME);
            conn = DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (java.sql.SQLException ex) {
            ex.printStackTrace();
        }
        return conn;
    }

   /* public static User executeQuery(String sql, Object... objs) {
        User user = new User();
        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; objs != null && i < objs.length; i++) {
                pstmt.setObject(i + 1, objs[i]);
            }
            rs = pstmt.executeQuery();
            while (rs.next()) {
                user.setPkId(rs.getString("pk_id"));
                user.setLoginName(rs.getString("login_name"));
                user.setPassword(rs.getString("password"));
                user.setNickName("nick_name");
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            close();
        }
        return user;
    }
*/
    /*public static List<User> getUsers(String sql, Object... objs) {
        List<User> users = new ArrayList<>();
        User user;
        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; objs != null && i < objs.length; i++) {
                pstmt.setObject(i + 1, objs[i]);
            }
            rs = pstmt.executeQuery();
            while (rs.next()) {
                user = new User();
                user.setPkId(rs.getString("pk_id"));
                user.setLoginName(rs.getString("login_name"));
                user.setPassword(rs.getString("password"));
                user.setNickName("nick_name");
                users.add(user);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            close();
        }
        return users;
    }
*/
   /* public static int executeUpdate(String sql, Object... objs) {
        conn = getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; objs != null && i < objs.length; i++) {
                pstmt.setObject(i + 1, objs[i]);
            }
            return pstmt.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static void saveUser(User user) {
        executeQuery("insert into t_user(pk_id, login_name,password,nick_name) values(?,?,?,?)",
                new Object[] { user.getPkId(), user.getLoginName(), user.getPassword(), user.getNickName() });
    }
*/
    public static void close() {
        try {
            if (pstmt != null)
                pstmt.close();
            if (rs != null)
                rs.close();
            if (conn != null)
                pstmt.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
